// pages/myDistribution/myDistribution.js
var common = require("../../utils/Conmon.js");
var appData = getApp().globalData;
var pageSize=10000;
var pageIndex=1;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    popup: false,
    HeadImg: null,
    UserId: null,
    NickName: null,
    FollowTime: '',
    Currencys: null,
    UserCommission: [],
    TotalMemeberCount: 0,
    searchKey: '',
    showLoading: false,
    TierMembers: null,
    _level:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    common.pageInit( function () {
    });
    var that = this;
    wx.showLoading({ title: '加载中...', mask: true });
    that.wxSubRequest('GET', 'member/api/MemberCenter/Distribution',{ userId: wx.getStorageSync('UserInfo').UserID, custId: appData.custID, isValid: 1 },
      function (data) {
        if (data.code == 1200) {
          wx.hideLoading()

          var currs = JSON.parse(data.content.UserCurrencyNameSet);
          if (currs != null && currs.length > 0) {
            for (var i = 0; i < currs.length; i++) {
              var Amount = (currs[i].NowAmount + currs[i].LiveAmount).toFixed(2);
              currs[i].Amount = Amount;
            }
          }

          var userCommission = JSON.parse(data.content.strArr);
          var totalCount = 0;
          if (userCommission != null && userCommission.length > 0) {
            for (var i = 0; i < userCommission.length; i++) {
              totalCount += userCommission[i].length;
            }
          }
          that.setData({
            HeadImg: data.content.HeadImg,
            UserId: data.content.UserId,
            NickName: data.content.UserName,
            FollowTime: data.content.UserinfoModel.userinfoApiDto == null ? '' : data.content.UserinfoModel.userinfoApiDto.FollowTime,
            Currencys: currs,
            UserCommission: userCommission,
            TotalMemeberCount: totalCount
          });
          //查询粉丝团队树
          var param = { userId: wx.getStorageSync('UserInfo').UserID, custId: appData.custID, pIndex: pageIndex, pageSize: pageSize, level: 0, searchid: "-9999" };
          that.searChGetDataLevel(param);
        }
        else {
          wx.hideLoading()
          wx.showModal({ title: '错误', content: data.msg, showCancel: false })
        }
      },
      function (obj) {
        wx.hideLoading()
        wx.showModal({ title: '错误', content: '加载异常错误...', showCancel: false })
        console.log(obj)
      })

  },

  /**
   * 选择粉丝层级
   */
  openChangeTap: function (event) {
    var that = this;
    var _level = event.currentTarget.dataset.level;
    that.setData({
      _level: _level
    })
    pageIndex=1;
    var param = { userId: wx.getStorageSync('UserInfo').UserID, custId: appData.custID, pIndex: pageIndex, pageSize: pageSize, level: _level, searchid: "-9999" };
    that.searChGetDataLevel(param);
  },

  /**
   * 查询框输入事件
   */
  bindSearchInput: function (event) {
    var val = event.detail.value;
    this.setData({
      searchKey: val
    })
  },

  /**
   * 点击查询粉丝
   */
  bindSearchClickTap: function (event) {
    var that = this;
    var val = that.data.searchKey.trim();
    var _obj;
    if (that.data.UserCommission != null && that.data.UserCommission.length>0){
      for (var i = 0; i < that.data.UserCommission.length; i++) {
        for (var j = 0; j < that.data.UserCommission[i];j++){
          if (val == that.data.UserCommission[i][j].UserID){
            _obj = that.data.UserCommission[i];
          }
        }
      }
    }
    
    if (_obj!=undefined)
    {
       that.setData({
         _level: _obj.Level
       })
    }else{
      that.setData({
        _level: 0
      })
    }
    pageIndex=1;
    var param = { userId: wx.getStorageSync('UserInfo').UserID, custId: appData.custID, pIndex: pageIndex, pageSize: pageSize, level: that.data._level, searchid: val == "" ? "-9999" : that.data.searchKey };
    that.searChGetDataLevel(param);
  },

  /**
   * 加载团队粉丝数
   */
  searChGetDataLevel: function (param) {
    var that = this;
    that.setData({
      showLoading: true,
      TierMembers:null
    })
    that.wxSubRequest('GET', 'member/api/MemberCenter/GetUserInfoByLevel', param,
      function (data) {
        if (data.code == 1200) {
          that.setData({
            showLoading: false
          })
          if (data.content != null) {
            that.setData({
              TierMembers: JSON.parse(data.content).Data
            })
          }
        } else {
          that.setData({
            showLoading: false
          })
          wx.showModal({ title: '错误', content: "加载失败...", showCancel: false })
        }
      },
      function (obj) {
        that.setData({
          showLoading: false
        })
        wx.showModal({ title: '错误', content:"加载失败...", showCancel: false })
        wx.showToast({
          title: '加载失败...',
          duration: 200
        })
      })
  },

  /**
   * 关闭弹出层
   */
  closePop: function () {
    this.setData({
      popup: false
    });
  },

  /**
   * 微信提交请求获取数据或保存数据
   */
  wxSubRequest: function (method, url, param, callBack, CallBackError) {
    wx.request({
      url: appData.apiurl+url,
      method: method,
      data: param,
      header: {
        "content-type": "application/json"
      },
      success: function (res) {
        if (res.statusCode == 200) {
          var data = res.data;
          callBack(data)
          console.log(res.data)
        }
      },
      fail: function (res) {
        CallBackError(res.data)
        console.log(res.data)
      },
      complete: function (res) {
        console.log(res.msg)
      }
    })
  },

  /***************************************************** */
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})